// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html


cc.Class({
    extends: cc.Component,

    properties: {
        className:"comparelayer",

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function () {
        this.node.thisJS = this;
        this.contentnode = cc.find("contentnode",this.node);
        this.rightpanel = cc.find("rightpanel",this.contentnode);
        this.leftpanel = cc.find("leftpanel",this.contentnode);
        this.bgnode = cc.find("bg",this.rightpanel);
        this.bgnode.runAction(cc.flipX(true))
        this.bgnode.runAction(cc.flipY(true))
        this.light = cc.find("light",this.contentnode);
        this.vs = cc.find("vs",this.contentnode);
        this.leftMask = cc.find("headmask",this.leftpanel);
        this.rightMask = cc.find("headmask",this.rightpanel);
        this.leftBai = cc.find("comLoser",this.leftpanel);
        this.rightBai = cc.find("comLoser",this.rightpanel);
        this.contentnode.active = false ;
        this.vs.active = false ;
        this.compareret = cc.find("compareret",this.contentnode);
        
    },
    start: function () {
        this.initUI();

    },
    initUI: function(){
        console.log("接收监听");
        EventManager.addEventObeserveF(EventConfig.clearTable, handler(this,
            this.clearTable),"comparelayer");
        EventManager.addEventObeserveF(EventConfig.compareNtf, handler(this,
            this.compareNtf),"comparelayer");
        EventManager.addEventObeserveF(EventConfig.roundOverCompareCard, handler(this,
            this.roundOverCompareCard),"comparelayer");

    },

    /*
    //cmd：1707 广播：用户比牌      
    message GoldFlowerUserCompareNtf{
        optional int32 uin = 1;                 //用户ID
        optional int32 desk_id=2;               //桌子ID
        optional int32 compare_uin=3;           //需要比牌玩家的uin
        optional int32 result=4;                //结果 1是发起比牌的人赢  0是发起比牌的人输
        optional int32 round_count=5;           //当前轮数
        optional int32 max_count=6;             //最大轮数
        optional int64 user_money=7;            //用户当前下的钱
        optional int64 desk_total_chip=8;       //桌子当前总注数
        optional int64 user_pay_real_money=9;   //用户实际出了多少钱(有可能是用户没钱了 强制比牌)
        optional int32 next_uin=10;             //下一个玩家(如果下一个玩家是自己，表示比牌玩家没钱了，需要一直比下去  为-1表示游戏结束)
        optional int32 can_operator = 11;       // 可以执行的操作 包括 看牌 比牌 弃牌  用位数表示  11  第一位看牌  第二位比牌 
        repeated int32 card = 12;               //用户手牌
        optional int32 card_type = 13;          //手牌类型
    }
    {
 53 　　cmd: 1707
 53 　　ret: 0
 53 　　sn: 0
 44 　　model: 
 38 　　{
 53 　　　　uin: 3603
 53 　　　　desk_id: 300027
 53 　　　　compare_uin: 3604
 53 　　　　result: 1
 53 　　　　round_count: 1
 53 　　　　max_count: 20
 53 　　　　user_money: 8
 53 　　　　desk_total_chip: 12
 53 　　　　user_pay_real_money: 4
 53 　　　　next_uin: -1
 53 　　　　can_operator: 11
 44 　　　　card: 
 38 　　　　{
 53 　　　　　　0: 40
 53 　　　　　　1: 55
 53 　　　　　　2: 42
 56 　　　　}
 53 　　　　card_type: 1
 56 　　}
 56 }
    */
    compareNtf:function(data){
        this.contentnode.active = true ;
        var info = GameData ;
        for(var k in info.playerinfo){
            if(info.playerinfo[k].uin==data.uin){
                var user = info.playerinfo[k];
                var searId = GameData.getUserSeatP(user.seatid);
                var head  =  cc.find("head",this.leftpanel);
                head.thisJS.initUserData({score:user.user_money || 0,
                    nick:user.nick});
            }
             if(info.playerinfo[k].uin==data.compare_uin){
                var user = info.playerinfo[k];
                var searId = GameData.getUserSeatP(user.seatid);
                var head  =  cc.find("head",this.rightpanel);
                head.thisJS.initUserData({score:user.user_money || 0,
                    nick:user.nick});
            }
         }
        var orgPos = this.leftpanel.getPosition();
        this.leftpanel.setPosition(cc.p(orgPos.x-300,orgPos.y));
        var move      = cc.moveTo(0.3,orgPos)
        var call  = cc.callFunc(function (){
             this.light.getComponent(cc.Animation).play();
             this.vs.active = true ;
        },this)
        var sq        = cc.sequence(move,call);
        this.leftpanel.runAction(sq)

        var orgPos = this.rightpanel.getPosition();
        this.rightpanel.setPosition(cc.p(orgPos.x+300,orgPos.y));
        var move      = cc.moveTo(0.3,orgPos)
        var delay     = cc.delayTime(1);
        var call  = cc.callFunc(function (){

            if(data.result == 1){
                this.compareret.setPosition(this.leftpanel.getPosition());
                this.leftMask.active = true ;
                this.leftBai.active = true ;
            }else{
                this.compareret.setPosition(this.rightpanel.getPosition());
                this.rightMask.active = true ;
                this.rightBai.active = true ;
            }
            this.light.getComponent(cc.Animation).stop();
            this.compareret.getComponent(cc.Animation).play();
        },this)
        var delay2     = cc.delayTime(1);
        var call2  = cc.callFunc(function (){
            this.leftMask.active = false ;
            this.rightMask.active = false ;
            this.contentnode.active = false ;
            this.leftBai.active = false ;
            this.rightBai.active = false ;
        },this)
        var sq        = cc.sequence(move,delay,call,delay2,call2);
        this.rightpanel.runAction(sq)  
    },
    /*
    message GoldFlowerRoundOverCompareCard{
        repeated int32 userlist = 1;        //比牌的两个玩家
        optional int32 win_uin = 2;             //赢的玩家的id
        repeated int32 card = 3;                //输家手牌
        optional int32 card_type = 4;           //输家手牌类型
    }
    */
    roundOverCompareCard:function(data){
       var result = 0 ;
       if (data.userlist[0] == data.win_uin){
            result = 1 ;
       }
       var info = {
        uin:data.userlist[0] ,
        compare_uin:data.userlist[1],
        result:result,
       }
       this.compareNtf(info);
    },
    //清桌
    clearTable:function(data){
     
    },

});
